%enhance the image contrast, only enhance the spot pixel
%p:grid image
function ctrenhance(p)
A=imread(p);
%t:multiple for enhancing the image contrast, which is automaticlly
%computed by the function contrast
t=contrast(A);
%imshow(A);title('source image');
A=double(A);
A=A./256;
[h,w,d]=size(A);
%repeat 10 times for random experiment��and take the mean as final result
for k=1:10
%background gry level 
m(1:12)=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%random select three positions at left edge
cx(1)=fix(rand(1)*h+1);
cx(2)=fix(rand(1)*h+1);
cx(3)=fix(rand(1)*h+1);
for i=1:3
  %estimate the background gray level,sacnning the edge by using a window
  %with size 10*10  
  if cx(i)<=5
    m(i)=max(max(A(cx(i):cx(i)+10,1:10)));
  elseif cx(i)+5>h
    m(i)=max(max(A(cx(i)-10:cx(i),1:10)));    
  else
    m(i)=max(max(A(cx(i)-5:cx(i)+5,1:10)));
  end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%random select three positions at bottom edge
cy(1)=fix(rand(1)*w+1);
cy(2)=fix(rand(1)*w+1);
cy(3)=fix(rand(1)*w+1);
for j=1:3
  if cy(j)<=5
    m(j+3)=max(max(A(1:10,cy(j):cy(j)+10)));
  elseif cy(j)+5>w
    m(j+3)=max(max(A(1:10,cy(j)-10:cy(j))));    
  else
    m(j+3)=max(max(A(1:10,cy(j)-5:cy(j)+5)));
  end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%random select three positions at right edge
cx(4)=fix(rand(1)*h+1);
cx(5)=fix(rand(1)*h+1);
cx(6)=fix(rand(1)*h+1);
for i=4:6
 if cx(i)<=5
    m(i+3)=max(max(A(cx(i):cx(i)+10,w-10:w)));
  elseif cx(i)+5>h
    m(i+3)=max(max(A(cx(i)-10:cx(i),w-10:w)));
  else
    m(i+3)=max(max(A(cx(i)-5:cx(i)+5,w-10:w)));
  end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%random select three positions at top edge
cy(4)=fix(rand(1)*w+1);
cy(5)=fix(rand(1)*w+1);
cy(6)=fix(rand(1)*w+1);
for j=4:6
  if cy(j)<=5
    m(j+6)=max(max(A(h-10:h,cy(j):cy(j)+10)));
  elseif cy(j)+5>w
    m(j+6)=max(max(A(h-10:h,cy(j)-10:cy(j))));
  else
    m(j+6)=max(max(A(h-10:h,cy(j)-5:cy(j)+5)));
  end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%estimate the final background gray level
edgeto(k)=min(m);
end
edget=mean(edgeto);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%enhance the image 
count=0;
for i=1:h
    for j=1:w
     if A(i,j)>edget   
        A(i,j)=A(i,j)*t;
        if A(i,j)>255
           A(i,j)=255;
        end
     end
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=uint8(A);
%figure,imshow(A);title('result after enhance the image contrast');
%reduce the noise by using median filter
C=zeros(h,w);
for i=2:h-1
    for j=2:w-1
       for k=-1:1
            for l=-1:1
                B(k*3+l+5)=A(i+k,j+l);
            end
        end    
        B1=sort(B);
        C(i,j)=B1(5);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C=C.*256;C=uint16(C);
%figure,imshow(C);title('median filter');
imwrite(C,'ftest.tif','tif');
clear A;
clear B;
clear B1;
clear C;
